package com.common.android.utils.httpRequest;

import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.json.JSONObject;

import com.product.android.business.bean.UserInfo;
import com.product.android.commonInterface.allCommonInterfaceImpl.AllCommonCallOtherModel;

import android.content.Context;
import android.text.TextUtils;

/**
 * @ClassName: AutoLoginHttpRequest
 * @Description: 自动登陆http请求类
 * @author Administrator
 * @date 2012-2-1 上午09:50:25
 */
public class AutoLoginHttpRequest extends OapHttpRequest {
    
    /** 
     * @ClassName: IOnReLogin 
     * @Description: 重新登陆回调接口
     * @author yanyy
     * @date 2012-12-14 下午06:13:39 
     *
     */
    public interface IOnReLogin{
        public void onLogin(UserInfo u);
    }

    private static IHttpRequest _instance;	// 只能私有，因为是static的，子类复用的时候可能导致转型错误。子类必须自己定义

	protected String mReLoginURL;// 正常来说，这个参数无值，只有401重登后才会有值
    
    protected AutoLoginHttpRequest(Context ctx, int appid, int type) {
    	super(ctx);
        mContext = ctx.getApplicationContext();
    }

    /**
     * @Title: getInstance  
     * @Description: 获取单例  
     * @author yanyy
     * @date 2012-12-14 下午06:13:56 
     *
     * @param ctx
     * @param appid uap应用标识
     * @param type  登陆类型(0 uap 1 oap)
     * @param onLogin 登陆回调
     * @return       
     * @return IHttpRequest
     * @throws
     */
    public static IHttpRequest getInstance(Context ctx, int appid, int type) {
        if (_instance == null) {
            _instance = new AutoLoginHttpRequest(ctx, appid, type);
        }
        return _instance;
    }
    
    @Override
    public int doDelete(String mStrURL, StringBuilder receive) {
        int ireturn = super.doDelete(mStrURL, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doDelete(mReLoginURL, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doDownFile(String mStrURL, File saveFile) {
        int ireturn = super.doDownFile(mStrURL, saveFile);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(new StringBuilder())){
                return super.doDownFile(mReLoginURL, saveFile);
            }
        }
        return ireturn;
    }

    @Override
    public int doGet(String mStrURL, StringBuilder receive) {
        int ireturn = super.doGet(mStrURL, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doGet(mReLoginURL, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doPost(String mStrURL, JSONObject c, StringBuilder receive) {
        int ireturn = super.doPost(mStrURL, c, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPost(mReLoginURL, c, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doPost(String mStrURL, String content, StringBuilder receive) {
        int ireturn = super.doPost(mStrURL, content, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPost(mReLoginURL, content, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doPost(String mStrURL, byte[] b, StringBuilder receive) {
        int ireturn = super.doPost(mStrURL, b, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPost(mReLoginURL, b, receive);
            }
        }
        return ireturn;
    }
    
	@Override
    public int doPost(String url, HttpEntity entity, StringBuilder receive) {
    	int ireturn = super.doPost(url, entity, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = url;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPost(mReLoginURL, entity, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doPut(String mStrURL, String content, StringBuilder receive) {
        int ireturn = super.doPut(mStrURL, content, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPut(mReLoginURL, content, receive);
            }
        }
        return ireturn;
    }

    @Override
    public int doPut(String mStrURL, JSONObject c, StringBuilder receive) {
        int ireturn = super.doPut(mStrURL, c, receive);
        if (ireturn == HttpStatus.SC_UNAUTHORIZED){
        	this.mReLoginURL = mStrURL;
            //需要重新登陆
            if (doReLoginOap(receive)){
                return super.doPut(mReLoginURL, c, receive);
            }
        }
        return ireturn;
    }

    /**
     * @Title: doReLoginOap  
     * @Description: 重新登陆
     * @author yanyy
     * @date 2012-12-14 下午06:13:27 
     *
     * @param errmsg
     * @return       
     * @return boolean
     * @throws
     */
    private boolean doReLoginOap(StringBuilder errmsg){
		String sid = AllCommonCallOtherModel.MainModel.getRelginSid();
		DoReplaceAfterRelgin(sid);
    	return !TextUtils.isEmpty(sid);
    }
    
    /**
     * 
     * @n<b>函数名称</b>     :DoReplaceAfterRelgin
     * @brief	用于url中带sid情况下替换，如果不满足的子类可以重写
     * @see
     * @since    Ver 1.1
     * @param  @param sid
     * @param  @return
     * @return String
     * @<b>作者</b>          :  huangszh
     * @<b>创建时间</b>      :  2014-3-6下午6:11:54
     */
    protected void DoReplaceAfterRelgin(String sid) {
    	int index = mReLoginURL.indexOf("sid=");
    	if(index != -1 && !TextUtils.isEmpty(sid)){
    		int end = mReLoginURL.indexOf("&",index + 4);
			StringBuilder url = new StringBuilder();
			url.append(mReLoginURL.subSequence(0, index));
			url.append("sid=");
			url.append(sid);
			if(end != -1){
				url.append(mReLoginURL.subSequence(end, mReLoginURL.length()));
			}
			mReLoginURL = url.toString();
    	}
	}
}
